package cn.hfatec.shms.web.members.ykDepartment.service;

import cn.hfatec.base.BaseDao;
import cn.hfatec.shms.web.members.ykDepartment.dao.YkDepartmentDao;
import cn.hfatec.shms.web.members.ykDepartment.model.YkDepartment;
import cn.hfatec.shms.web.members.ykDepartment.model.YkDepartmentParams;
import cn.hfatec.shms.web.members.ykDepartment.model.YkDepartmentView;
import com.google.common.collect.Lists;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;


/**
 *
 * @author hfatec
 * @version 1.0
 *
 * the JAVA code is generate by middlegen
 *
 */
@Service("YkDepartmentService")
public class YkDepartmentService {

    public static final String BEAN_ID = "YkDepartmentService";

    //注入Dao层
    @Resource(name = "YkDepartmentDao")
    private YkDepartmentDao ykDepartmentDao;

    //注入BaseDao
    @Resource(name = "BaseDao")
    private BaseDao baseDao;

    /**
     *
     * 查询数据
     *
     * @param ykDepartmentParams
     *
     *
     */
    @Transactional
    public List queryYkDepartmentTable(YkDepartmentParams ykDepartmentParams){

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("params", ykDepartmentParams);
        return baseDao.queryList("queryYkDepartment",params);
    }

    /**
     *
     * 插入数据
     * @param ykDepartment
     *
     */
    @Transactional
    public void saveYkDepartment(YkDepartment ykDepartment) {
        if(ykDepartment.getId() != null) {
            YkDepartment result = getYkDepartment(ykDepartment.getId());
            if(result != null) {
                ykDepartmentDao.update(ykDepartment);
            } else {
                ykDepartmentDao.insert(ykDepartment);
            }
        } else {
            ykDepartmentDao.insert(ykDepartment);
        }
    }

    /**
     *
     * 删除数据
     * @param id
     *
     */
    @Transactional
    public void deleteYkDepartment(Integer id) {

        ykDepartmentDao.delete(id);
    }

    /**
     *
     * 删除数据
     * @param ykDepartmentList
     *
     */
    @Transactional
    public void deleteYkDepartmentBatch(List<String> ykDepartmentList) {
        for(String id : ykDepartmentList) {
            deleteYkDepartment(Integer.valueOf(id));
        }
    }

    /**
     *
     * 根据ID获取数据
     * @param id
     *
     */
    @Transactional
    public YkDepartment getYkDepartment(Integer id) {

        return ykDepartmentDao.getYkDepartmentById(id);
    }

    /**
     * 根据父节点ID、总菜单数据，递归获取机构视图信息
     *
     * @param parentOrgId
     * @param allOrgList
     * @return
     * @throws java.lang.reflect.InvocationTargetException
     * @throws IllegalAccessException
     */
    @Transactional
    public List<YkDepartmentView> getOrgList(String parentOrgId, List<YkDepartment> allOrgList) throws InvocationTargetException, IllegalAccessException {
        List<YkDepartmentView> list = Lists.newArrayList();
        for (YkDepartment org : allOrgList) {
            if (org.getParentOrgId() != null && org.getParentOrgId().equals(parentOrgId)) {
                YkDepartmentView orgView = new YkDepartmentView();
                BeanUtils.copyProperties(orgView, org);
                orgView.setChildrenOrgList(getOrgList(orgView.getOrgId(), allOrgList));
                list.add(orgView);
            }
        }
        return list;
    }
}